﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.ApproveCommand;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.ApproveCommandHandler
{
    /// <summary>
    /// 删除申请--Id
    /// </summary>
    public class DeleteLeaveModelCommandHandler : IRequestHandler<DeleteLeaveModelCommand, APIResult<string>>
    {
        private readonly IBaseRepository<LeaveModel> leaveRepos;

        public DeleteLeaveModelCommandHandler(IBaseRepository<LeaveModel> leaveRepos)
        {
            this.leaveRepos = leaveRepos;
        }

        /// <summary>
        /// 删除申请--Id
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<string>> Handle(DeleteLeaveModelCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            var delobj = await leaveRepos.GetAll().Result.FirstOrDefaultAsync(x => x.LeaveId == request.LeaveId);
            if (delobj != null)
            {
                var res = await leaveRepos.Delete(delobj);
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = res.ToString();
            }else
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
                result.data = "";
            }           
            return await Task.FromResult(result);
        }
    }
}
